package android.Spider;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UserProfile extends Activity
{

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		SetContentView();
		
        m_dlgProgress = ProgressDialog.show(this, "Fetching data", "Please wait for XMPP connection", true, true);

        m_connection.Initialize(this);

		SetCurrentSettings();
	}
	
	protected void SetContentView()
	{
        setContentView(R.layout.profile);
	}
	
	@Override
	protected void onDestroy()
	{
		if (m_brMother != null)
			m_brMother.UpdateProfileInfo();
		
		if (m_dlgProgress != null)
			m_dlgProgress.cancel();

		m_connection.Unregister();
		super.onDestroy();
	}

	protected void SetCurrentSettings()
	{
		if (m_dlgProgress == null && m_usrProfile != null && SpiderListener.UserName.length() > 0)
		{
			((TextView)findViewById(R.id.ProfileFirstName)).setText(m_usrProfile.m_strFirstName);
			((TextView)findViewById(R.id.ProfileLastName)).setText(m_usrProfile.m_strLastName);
			((TextView)findViewById(R.id.ProfilePassword)).setText(m_usrProfile.m_strPassword);
			((TextView)findViewById(R.id.ProfileRePassword)).setText(m_usrProfile.m_strPassword);
			((TextView)findViewById(R.id.ProfileGMailAccount)).setText(SpiderListener.UserName);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    
	    menu.add(0, 0, "Submit", R.drawable.submitsend);
	    menu.add(0, 1, "Details", R.drawable.profiledetails);
	    menu.add(0, 2, "Discard", R.drawable.discard);
	    return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras)
	{
		if (resultCode == 1)
			finish();
		
		super.onActivityResult(requestCode, resultCode, data, extras);
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
		m_usrProfile.m_strFirstName = ((EditText)findViewById(R.id.ProfileFirstName)).getText().toString();
		m_usrProfile.m_strLastName = ((EditText)findViewById(R.id.ProfileLastName)).getText().toString();
		m_usrProfile.m_strPassword = ((EditText)findViewById(R.id.ProfilePassword)).getText().toString();

		if (item.getId() == 0)
		{
    		String strRePassword = ((EditText)findViewById(R.id.ProfileRePassword)).getText().toString();
    		
    		if (m_usrProfile.m_strPassword.equals(strRePassword))
    		{
    			m_connection.SubmitProfile(m_usrProfile);
        		m_connection.Unregister();
        		finish();

        		return true;
    		}
    		else
    		{
    			Toast.makeText(UserProfile.this, "Please check your password", Toast.LENGTH_SHORT).show();
    		}
		}
		else if (item.getId() == 1)
		{
			Intent intent = new Intent(this, UserProfileDetails.class);
			Bundle bundle = m_usrProfile.toBundle();
			intent.putExtras(bundle);
			startSubActivity(intent, 0);
			return true;
		}
		else if (item.getId() == 2)
		{
			finish();
			return true;
		}
		
		return false;
	}
	
	XmppConnection m_connection = new XmppConnection()
    {
		@Override
		public void OnConnected()
		{
			if (m_dlgProgress != null)
			{
				m_dlgProgress.cancel();
				m_dlgProgress = null;
			}

			m_usrProfile = SpiderPreferences.Instance(UserProfile.this).GetUserProfile();
			SetCurrentSettings();
		}

    };
    
    ProgressDialog m_dlgProgress = null;
    ProfileUser m_usrProfile = null;
    static Broodmother m_brMother = null;

}
